package cn.xyy.common.domain.mvc;

/**
 * 
 * 响应实体类 要加上泛型，不然fegin调用方返回的是对象变成LinkedHashMap
 * 
 * @author xyy
 *
 * @param <T>
 */
public class R<T> {
	// ok
	public static final int CODE_200 = 200;
	// error
	public static final int CODE_500 = 500;

	private int code = CODE_200;
	private String message;
	private T result;

	public R() {

	}

	public R(int code, String msg, T result) {
		this.setCode(code);
		this.setMessage(msg);
		this.setResult(result);
	}

	public static <T> R<T> ok() {
		return result(CODE_200, "success", null);
	}

	public static <T> R<T> ok(T result) {
		return result(CODE_200, "success", result);
	}

	public static <T> R<T> error() {
		return result(CODE_500, "error", null);
	}

	public static <T> R<T> error(String msg) {
		return result(CODE_500, msg, null);
	}

	public static <T> R<T> error(int code, String msg) {
		return result(code, msg, null);
	}

	public static <T> R<T> result(int code, String msg, T result) {
		R<T> r = new R<>();
		r.setCode(code);
		r.setResult(result);
		r.setMessage(msg);
		return r;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public T getResult() {
		return result;
	}

	public void setResult(T result) {
		this.result = result;
	}

}
